package com.iot.ic.service;



import com.iot.ic.common.Options;
import com.iot.ic.common.Response;
import com.iot.ic.dao.FaultSupportMapper;
import com.iot.ic.model.Device;
import com.iot.ic.model.FaultSupport;
import com.iot.ic.model.MatchPause;
import com.iot.ic.model.StadiumResult;
import com.iot.ic.model.TeamInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiuge on 2017/2/20.
 */
@Service
public class FaultSupportServiceImpl implements  FaultSupportService {

	
    @Autowired
    private DeviceService deviceService;
	
	@Autowired
	private FaultSupportMapper faultSupportMapper;
	
	@Autowired
	private StadiumResultService stadiumResultService;
	
	@Autowired
	private TeamService teamService;
    @Autowired
    private MatchPauseService matchPauseService;
    
    @Autowired
  	SimpMessagingTemplate simpMessagingTemplate;
	
	@Override
	public int addFaultSupportInfo(FaultSupport faultSupport) {
		faultSupport.setCreateTime(new Date());
		faultSupport.setDelFlag(Options.NotDel.getCode());
		faultSupport.setFaultSupportState(0);
		int result = faultSupportMapper.insert(faultSupport);
		
		//结束比赛
		StadiumResult stadiumResult = new StadiumResult();
		stadiumResult.setStadiumResultId(faultSupport.getStadiumResultId());
		stadiumResult.setEndTime(new Date());
		stadiumResult.setMatchState(Options.Finished.getCode());
		
		MatchPause matchPause = new MatchPause();
		matchPause.setStadiumResultId(faultSupport.getStadiumResultId());
		List<MatchPause> matchPauseList =  matchPauseService.listMatchPause(matchPause);
		int pauseTime = 0;
		for (MatchPause matchPauseData : matchPauseList) {
			long startTime = matchPauseData.getStartTime().getTime();
			long endTime = matchPauseData.getEndTime().getTime();
			int pauseTimeData = (int) (endTime - startTime)/1000;
			pauseTime = pauseTime + pauseTimeData;
		}
		StadiumResult oldStadiumResult = stadiumResultService.getStadiumResult(faultSupport.getStadiumResultId());
		long startTime = oldStadiumResult.getStartTime().getTime();
		long endTime = stadiumResult.getEndTime().getTime();
		int matchTime = (int) ((endTime - startTime)/1000);
		matchTime = matchTime - pauseTime;
		stadiumResult.setMatchTime(matchTime);
		
		stadiumResultService.updateStadiumResult(stadiumResult);
		
		//修改车队状态
		TeamInfo team = new TeamInfo();
		team.setTeamState(Options.Fault.getCode());
		team.setTeamId(faultSupport.getTeamId());
		teamService.updateTeam(team);
		
		//websocket推送
		Map<String,Integer> teamStateMap = new HashMap<String,Integer>();
		teamStateMap.put("teamState", Options.Fault.getCode());
		simpMessagingTemplate.convertAndSendToUser(faultSupport.getTeamId().toString(), "/testTeamStatesocketpush", teamStateMap);
		
		Response response=new Response();
		List<Map<String,Object>> list = stadiumResultService.getStadiumResultDetailByYear(2017);
		response.setResult(list);
		simpMessagingTemplate.convertAndSend("/topic/testStadiumResultSocketpush", response);
		if (oldStadiumResult.getStadiumId() == Options.CityStadium.getCode()) {
			List<Map<String,Object>> stadiumDataResultList = stadiumResultService.getStadiumResultDetailByStadiumType(1);
			response.setResult(stadiumDataResultList);
			simpMessagingTemplate.convertAndSend("/topic/testStadiumResult1Socketpush", response);
		} else if (oldStadiumResult.getStadiumId() == Options.HighSpeedStadium.getCode()) {
			List<Map<String,Object>> stadiumDataResultList = stadiumResultService.getStadiumResultDetailByStadiumType(2);
			response.setResult(stadiumDataResultList);
			simpMessagingTemplate.convertAndSend("/topic/testStadiumResul2Socketpush", response);
		} else if (oldStadiumResult.getStadiumId() == Options.Suburbs.getCode()) {
			List<Map<String,Object>> stadiumDataResultList = stadiumResultService.getStadiumResultDetailByStadiumType(3);
			response.setResult(stadiumDataResultList);
			simpMessagingTemplate.convertAndSend("/topic/testStadiumResul3Socketpush", response);
		}
		
		//解除设备绑定
		List<Device> deviceList = deviceService.getDeviceListByTeamId(faultSupport.getTeamId());
		for (Device device : deviceList) {
			deviceService.unBindDeviceWithTeam(device.getDeviceId());
		}
		
		
		return result;
	}

	@Override
	public List<Map<String, Object>> getFaultSupport(int faultSupportState) {
		FaultSupport faultSupport=new FaultSupport();
		faultSupport.setFaultSupportState(faultSupportState);
		List<Map<String,Object>> list=faultSupportMapper.selectFaultSupportList(faultSupport);
		return list;
	}

	@Override
	public List<Map<String,Object>> getFaultSupportByTeam(FaultSupport faultSupport){
		List<Map<String,Object>> list=faultSupportMapper.selectFaultSupportList(faultSupport);
		return list;
	}

	@Override
	public int handleFaultSupport(int faultSupportId) {
		FaultSupport faultSupport = faultSupportMapper.selectByPrimaryKey(faultSupportId);
		faultSupport.setSupportTime(new Date());
		faultSupport.setFaultSupportState(1);
		int result = faultSupportMapper.updateByPrimaryKeySelective(faultSupport);
		
		//修改车队状态
		TeamInfo team = new TeamInfo();
		team.setTeamState(Options.Finish.getCode());
		team.setTeamId(faultSupport.getTeamId());
		teamService.updateTeam(team);
		
		//websocket推送
		Map<String,Integer> teamStateMap = new HashMap<String,Integer>();
		teamStateMap.put("teamState", Options.Finish.getCode());
		simpMessagingTemplate.convertAndSendToUser(faultSupport.getTeamId().toString(), "/testTeamStatesocketpush", teamStateMap);
		
		return result;
	}

	/**
	 * 删除人工干预
	 *
	 * @param faultSupportId
	 * @return
	 */
	@Override
	public int delFaultSuppoert(int faultSupportId) {
		FaultSupport faultSupport=new FaultSupport();
		faultSupport.setFaultSupportId(faultSupportId);
		faultSupport.setDelFlag(Options.Deleted.getCode());
		int result=faultSupportMapper.updateByPrimaryKeySelective(faultSupport);
		return result;
	}

	@Override
	public FaultSupport getFaultSuppoertByTeamId(int teamId) {
		FaultSupport faultSupport = faultSupportMapper.getFaultSupportByTeamId(teamId);
		return faultSupport;
	}
	
	


}
